#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main( int argc, char **argv )
{
   pid_t pid;

   /* Turn off SIGHUP Handler */
   signal( SIGHUP, SIG_IGN );

   /* Fork to run a Java Process */
   pid = fork();

   switch( pid )
   {
      case( 0 ):
      {
         /* Java Child Process */
         ( void )( setsid() );

         argv[ 0 ] = "/usr/bin/java";

         execvp( argv[ 0 ], argv );

         exit( -1 );
      }
      
      case( -1 ):
      {
         /* Whoops! */
         printf( "Could not run %s.\n", argv[ 0 ] );

         exit( -1 );

         break;
      }

      default:
      {
         int ret;

         /* Parent Process */
         printf( "Started Java Daemon (pid: %i)\n", ( int )( pid ) );

         /* Catch the exit code from the Java Daemon Process */
         sleep( 1 );
         if( ( waitpid( pid, &ret, WNOHANG ) == pid ) &&
             ( ret != 0 ) )
         {
            printf( "Exit code (%i)\n",  WEXITSTATUS( ret ) );

            exit( -1 );
         }
      
         break;
      }
   }
      
   exit( 0 );
}

/* end of source file */
